#!/usr/bin/perl -w
use strict;

my $parse_fa_file_string = "bwa_sam_fa_file";
my $parse_tmp_file_path_string = "bwa_sam_tmp";
my $parse_wgsim_file_string = "bwa_sam_wgsim";
my $parse_wgsim_args_string= "bwa_sam_wgsim_args";
my $parse_hadoop_exe_string = "bwa_sam_hadoop";
my $parse_hadoop_input_url_string = "bwa_sam_hadoop_in";
my $parse_hadoop_out_bwa_url_string = "bwa_sam_hadoop_out_bwa";
my $parse_script_path_string = "bwa_sam_script_path";
my $parse_hadoop_streaming_jar_string = "bwa_sam_hadoop_streaming_jar";
my $parse_bwa_exe_string = "bwa_sam_bwa_exe";
my $parse_hadoop_out_sam_url_string = "bwa_sam_sam_out";
my $parse_sam_exe_string = "bwa_sam_sam_exe";
my $parse_hadoop_bam_int_url_string = "bwa_sam_bam_int";

my $fa_file_path;
my $tmp_file_path;
my $wgsim_file;
my $wgsim_args;
my $hadoop_exe;
my $hadoop_input_url;
my $hadoop_out_bwa_url;
my $hadoop_out_sam_url;
my $script_path;
my $hadoop_streaming_jar;
my $bwa_exe;
my $sam_exe;
my $hadoop_bam_int_url;

my $config_fh;
my $config_fn;
my $numArgs = $#ARGV + 1;
my $merge_fn = "merge.fq";
my $find_var_fn = "findvar.file";

sub set_env($$){
	my ($key, $val) = @_;
#	system("$sp/export.sh $key $val")== 0 or die "Could not execute $!\n";
	$ENV{'$key'} = $val;
	print "** $key: $ENV{'$key'} **";  
}

if($numArgs != 1){
	print "Wrong usage\n";
	exit(0);
}
my $dashes = "---------------\n";
print "$dashes Verifying arguments in config file .....\n$dashes";
$config_fn =$ARGV[0];
system("cp $ARGV[0] /tmp/$ARGV[0]")  == 0 or die "Could not execute $!\n";
open $config_fh,$config_fn or die("Can not open at $!\n");
my @contents = <$config_fh>;
foreach my $line(@contents){
	chomp($line);
	if($line =~ /($parse_fa_file_string)(=)(.*)/){
		$fa_file_path = $3;
		print "fa_file_path: $fa_file_path\n";
	}
	elsif($line =~ /($parse_tmp_file_path_string)(=)(.*)/){
                $tmp_file_path = $3;     
		print "tmp_file_path: $tmp_file_path\n";
        }
        elsif($line =~ /($parse_wgsim_file_string)(=)(.*)/){
                $wgsim_file = $3;     
                print "wgsim_file: $wgsim_file\n";
        }
	elsif($line =~ /($parse_wgsim_args_string)(=)(.*)/){
                $wgsim_args = $3;     
                print "wgsim_args: $wgsim_args\n";
        }
	elsif($line =~ /($parse_hadoop_exe_string)(=)(.*)/){
                $hadoop_exe = $3;
                print "hadoop_exe: $hadoop_exe\n";
        }
	elsif($line =~ /($parse_hadoop_input_url_string)(=)(.*)/){
                $hadoop_input_url = $3;
                print "hadoop_input_url: $hadoop_input_url\n";
        }
	elsif($line =~ /($parse_script_path_string)(=)(.*)/){
                $script_path = $3;
                print "script_path: $script_path\n";
        }
        elsif($line =~ /($parse_hadoop_streaming_jar_string)(=)(.*)/){
                $hadoop_streaming_jar = $3;
                print "script_path: $script_path\n";
        }
	elsif($line =~ /($parse_hadoop_out_bwa_url_string)(=)(.*)/){
                $hadoop_out_bwa_url = $3;
                print "hadoop_out_bwa_url: $hadoop_out_bwa_url\n";
        }
        elsif($line =~ /($parse_bwa_exe_string)(=)(.*)/){
                $bwa_exe = $3;
                print "hadoop_exe: $bwa_exe\n";
        }
        elsif($line =~ /($parse_hadoop_out_sam_url_string)(=)(.*)/){
                $hadoop_out_sam_url = $3;
                print "hadoop_out_sam_url: $hadoop_out_sam_url\n";
        }
        elsif($line =~ /($parse_sam_exe_string)(=)(.*)/){
                $sam_exe = $3;
                print "sam_exe: $sam_exe\n";
        }
	elsif($line =~ /($parse_hadoop_bam_int_url_string)(=)(.*)/){
		$hadoop_bam_int_url = $3;
		print "hadoop_bam_int_url: $hadoop_bam_int_url\n";
	}
}
close $config_fh;

if(!defined $tmp_file_path){
	print "No tmp specified in config file!\n";
	exit(0);
}

if(!defined $wgsim_file){
        print "No wgsim specified in config file!\n";
        exit(0);
}

if(!defined $fa_file_path){
        print "No Fasta file specified in config file!\n";
        exit(0);
}

if(!defined $wgsim_args){
        print "missig wgsim_args in config file!\n";
        exit(0);
}

if(!defined $hadoop_input_url){
        print "missig hadoop_in in config file!\n";
        exit(0);
}

if(!defined $hadoop_exe){
        print "missig hadoop_exe in config file!\n";
	exit(0);
}

if(!defined $script_path){
        print "missig script_path in config file!\n";
        exit(0);
}

if(!defined $hadoop_streaming_jar){
        print "missig hadoop_streaming_jar in config file!\n";
        exit(0);
}

if(!defined $hadoop_out_bwa_url){
        print "missig hadoop_out_bwa_url in config file!\n";
        exit(0);
}

if(!defined $hadoop_out_sam_url){
        print "missig hadoop_out_sam_url in config file!\n";
        exit(0);
}

if(!defined $bwa_exe){
        print "missig bwa_exe in config file!\n";
        exit(0);
}

if(!defined $sam_exe){
        print "missig sam_exe in config file!\n";
        exit(0);
}

if(!defined $hadoop_bam_int_url){
        print "missig sam_exe in config file!\n";
        exit(0);
}

my $wgsim_r1_fq_name = $tmp_file_path."out.r1.fq";
my $wgsim_r2_fq_name = $tmp_file_path."out.r2.fq";
my $wgsim_report_fn = $tmp_file_path."out.report.txt";

print "$dashes Sanitising....\n$dashes";

system("$hadoop_exe dfs -rmr $hadoop_input_url");
system("$hadoop_exe dfs -rmr $hadoop_out_bwa_url");
system("$hadoop_exe dfs -rmr $hadoop_out_sam_url");
system("$hadoop_exe dfs -rmr $hadoop_bam_int_url");
#system("rm -rf $tmp_file_path");
#system("mkdir $tmp_file_path");

print "$dashes Pre-process....\n$dashes";

system("$wgsim_file $wgsim_args $fa_file_path $wgsim_r1_fq_name $wgsim_r2_fq_name > $wgsim_report_fn") == 0 or die "Could not execute $!\n";
system("time $script_path/combine_fq.pl $wgsim_r1_fq_name $wgsim_r2_fq_name $tmp_file_path$merge_fn") == 0 or die "Could not execute $!\n";
system("$hadoop_exe  fs -put $tmp_file_path$merge_fn $hadoop_input_url") == 0 or die "Could not execute $!\n";

print "$dashes Running BWA....\n$dashes";

system("time $hadoop_exe jar $hadoop_streaming_jar -input $hadoop_input_url -output $hadoop_out_bwa_url -mapper $script_path/wrap_align.pl") == 0 or die "Could not execute $!\n";

print "$dashes Aligning....\n$dashes";

system("time $hadoop_exe jar $hadoop_streaming_jar -input $hadoop_out_bwa_url -output $hadoop_out_sam_url -mapper $script_path/wrap_sam.pl -reducer $script_path/red.pl -jobconf mapred.reduce.tasks=1")== 0 or die "Could not execute $!\n";
print "$dashes Finding Variations....\n$dashes";

my $find_var_counter = 0;
while(-e $tmp_file_path.$find_var_counter.$find_var_fn){
	$find_var_counter++;
}
system("$script_path/find_var.pl  $tmp_file_path/pileup.txt  $tmp_file_path/out.report.txt $tmp_file_path/$find_var_counter$find_var_fn") == 0 or die "Could not execute $!\n";

print "$dashes Done!....\n$tmp_file_path/$find_var_counter$find_var_fn generated!\n$dashes";

